package nc.impl.pub.service;

import java.util.ArrayList;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.itf.pub.dbservice.IDBService;
import nc.itf.pub.dbservice.INCPushCZService;
import nc.jdbc.framework.generator.IdGenerator;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;
import nccloud.bs.log.NCPushCZKJKMVO;
import nccloud.bs.log.NCPushCZfzhsVO;
import nccloud.bs.log.NCPushCZfzhsysVO;

public class NCPushCZServiceImpl implements INCPushCZService{
	private BaseDAO dao;
	private IDBService dbService;
	@Override
	public void insertFZHSVOs(JSONArray arrs) throws BusinessException {
		ArrayList<NCPushCZfzhsVO> lists = new ArrayList<>();
		for(int index =0 ; index < arrs.size() ; index++ ) {
			NCPushCZfzhsVO fzhsvo = new NCPushCZfzhsVO();
			fzhsvo.setStatus(1);
			JSONObject json = arrs.getJSONObject(index);
			fzhsvo.setAttributeValue("pk_fzhs", NCLocator.getInstance().lookup(IdGenerator.class).generate());
			fzhsvo.setAttributeValue("pk_group", "0001A210000000000JB9");
			fzhsvo.setAttributeValue("pk_org", "0001A210000000003BSQ");
			fzhsvo.setAttributeValue("pk_org_v", "0001A210000000003BSP");
			fzhsvo.setAttributeValue("pk_billtypeid", "assistHS");
			fzhsvo.setAttributeValue("vbillcode", json.getString("CODE"));
			fzhsvo.setAttributeValue("vbillname", json.getString("ELEMENT_NAME"));
			fzhsvo.setAttributeValue("sobillno", json.getString("ELEMENT_CODE"));
			fzhsvo.setAttributeValue("sobillname", json.getString("ELEMENT_NAME"));
			fzhsvo.setAttributeValue("vdef1", json.getString("DATA_ID"));
			fzhsvo.setAttributeValue("creationtime", new UFDateTime());
			fzhsvo.setAttributeValue("dr", "0");
			lists.add(fzhsvo);
		}
		if(!lists.isEmpty()) {
			this.getDbService().insertVOArray(lists.toArray(new NCPushCZfzhsVO[lists.size()]));
		}
		
	}

	@Override
	public Map<String, Object> queryFZHSVOByCode(Object fzhsCode) throws BusinessException {
		Object obj = this.getDao().executeQuery("select * from sync_fzhs where dr = 0 and vbillcode = '"+fzhsCode+"'", new MapProcessor());
		return obj == null ? null : (Map<String, Object>)obj;
	}

	@Override
	public void insertFZHSYSVOs(JSONArray arrs) throws BusinessException {
		ArrayList<NCPushCZfzhsysVO> lists = new ArrayList<>();
		for(int index =0 ; index < arrs.size() ; index++ ) {
			NCPushCZfzhsysVO fzhsvo = new NCPushCZfzhsysVO();
			fzhsvo.setStatus(1);
			JSONObject json = arrs.getJSONObject(index);
			fzhsvo.setAttributeValue("pk_fzhsys", NCLocator.getInstance().lookup(IdGenerator.class).generate());
			fzhsvo.setAttributeValue("pk_group", "0001A210000000000JB9");
			fzhsvo.setAttributeValue("pk_org", "0001A210000000003BSQ");
			fzhsvo.setAttributeValue("pk_org_v", "0001A210000000003BSP");
			fzhsvo.setAttributeValue("pk_billtypeid", "assistHS");
			fzhsvo.setAttributeValue("vbillcode", json.getString("CODE"));
			fzhsvo.setAttributeValue("vbillname", json.getString("ELEMENT_NAME"));
			fzhsvo.setAttributeValue("sobillno", json.getString("ELEMENT_CODE"));
			fzhsvo.setAttributeValue("sobillname", json.getString("ELEMENT_NAME"));
			fzhsvo.setAttributeValue("creationtime", new UFDateTime());
			fzhsvo.setAttributeValue("vdef1", json.getString("DATA_ID"));
			fzhsvo.setAttributeValue("dr", "0");
			lists.add(fzhsvo);
		}
		if(!lists.isEmpty()) {
			this.getDbService().insertVOArray(lists.toArray(new NCPushCZfzhsysVO[lists.size()]));
		}
	}

	@Override
	public Map<String, Object> queryFZHSYSVOByCode(Object fzhsysCode) throws BusinessException {
		Object obj = this.getDao().executeQuery("select * from sync_fzhsys where dr = 0 and vbillcode = '"+fzhsysCode+"'", new MapProcessor());
		return obj == null ? null : (Map<String, Object>)obj;
	}

	@Override
	public void insertKJKMVOs(JSONArray arrs) throws BusinessException {
		ArrayList<NCPushCZKJKMVO> lists = new ArrayList<>();
		for(int index =0 ; index < arrs.size() ; index++ ) {
			NCPushCZKJKMVO fzhsvo = new NCPushCZKJKMVO();
			fzhsvo.setStatus(1);
			JSONObject json = arrs.getJSONObject(index);
			fzhsvo.setAttributeValue("pk_fzhsys", NCLocator.getInstance().lookup(IdGenerator.class).generate());
			fzhsvo.setAttributeValue("pk_group", "0001A210000000000JB9");
			fzhsvo.setAttributeValue("pk_org", "0001A210000000003BSQ");
			fzhsvo.setAttributeValue("pk_org_v", "0001A210000000003BSP");
			fzhsvo.setAttributeValue("pk_billtypeid", "accountSubject");
			fzhsvo.setAttributeValue("vbillcode", json.getString("GOV_ACCT_CLS_CODE"));
			fzhsvo.setAttributeValue("vbillname", json.getString("GOV_ACCT_CLS_NAME"));
			fzhsvo.setAttributeValue("sobillno", json.getString("GOV_ACCT_CLS_CODE"));
			fzhsvo.setAttributeValue("sobillname", json.getString("GOV_ACCT_CLS_NAME"));
			fzhsvo.setAttributeValue("creationtime", new UFDateTime());
			fzhsvo.setAttributeValue("vdef1", json.getString("ACCOUNT_CLS_ID"));
			fzhsvo.setAttributeValue("dr", "0");
			lists.add(fzhsvo);
		}
		if(!lists.isEmpty()) {
			this.getDbService().insertVOArray(lists.toArray(new NCPushCZKJKMVO[lists.size()]));
		}
	}

	@Override
	public Map<String, Object> queryKJKMVOByCode(Object kjkmCode) throws BusinessException {
		Object obj = this.getDao().executeQuery("select * from sync_kjkm where dr = 0 and vbillcode = '"+kjkmCode+"'", new MapProcessor());
		return obj == null ? null : (Map<String, Object>)obj;
	}

	@Override
	public Object queryFZHSVOLastIndex() throws BusinessException {
		String sql = "select substr(sobillno,7) lastindex from ( select * from sync_fzhs order by to_number(substr(sobillno,7)) desc ) where rownum = 1";
		return this.getDao().executeQuery(sql, new ColumnProcessor());
	}

	@Override
	public Object queryFZHSYSVOLastIndex() throws BusinessException {
		// TODO Auto-generated method stub
		String sql = "select substr(sobillno,7) lastindex from ( select * from sync_fzhsys order by to_number(substr(sobillno,7)) desc ) where rownum = 1";
		return this.getDao().executeQuery(sql, new ColumnProcessor());
	}

	@Override
	public Object queryKJKMVOLastIndex() throws BusinessException {
		// TODO Auto-generated method stub
		String sql = "select substr(sobillno,7) lastindex from ( select * from sync_kjkm order by to_number(substr(sobillno,7)) desc ) where rownum = 1";
		return this.getDao().executeQuery(sql, new ColumnProcessor());
	}
	public BaseDAO getDao() {
		return this.dao == null ? new BaseDAO() : this.dao;
	}

	public IDBService getDbService() {
		if(null == this.dbService) {
			this.dbService = NCLocator.getInstance().lookup(IDBService.class);
		}
		return dbService;
	}

	
}
